local userId = KEYS[1]
local userId_2 = KEYS[2]

local orderId = ARGV[1]
local aliveTimes = tonumber(ARGV[2])
local aliveSeconds = ARGV[3]
local orderJsonStr = ARGV[4]

local currRawValue = tonumber(redis.call('get', userId) or '0')
if currRawValue < 1 then
    redis.call('incrby', userId, 1)
    redis.call('expire', userId, aliveSeconds)
    redis.call('hset', userId_2, orderId, orderJsonStr)
    return 1
elseif currRawValue >= aliveTimes then
    return 0
else
    redis.call('incrby', userId, 1)
    redis.call('hset', userId_2, orderId, orderJsonStr)
    return 1
end